import {Component, OnInit} from '@angular/core';
import {CommonService} from "../../service/base/common.service";
import {NzModalService} from "ng-zorro-antd/modal";
import {UserHttpService} from "../../service/http/user.http.service";
import {HttpCustomConfig} from "../../service/base/base.http.service";
import {UserDetailComponent} from "../../dialog/user-detail/user-detail.component";

@Component({
  selector: 'app-user-cps',
  templateUrl: './user-cps.component.html',
  styleUrls: ['./user-cps.component.less']
})
export class UserCpsComponent implements OnInit {

  dataList: any[] = [];
  pageIndex: number = 1;
  pageSize: number = 15;
  pageTotal: number = 0;
  mobile: string = '';
  userId: string = '';

  constructor(private commonService: CommonService,
              private modalService: NzModalService,
              private userHttpService: UserHttpService) {
  }

  defaultHttpConfig: HttpCustomConfig = {showLoading: false}

  ngOnInit() {
    this.queryData()
  }

  queryDataInit(): void {
    this.pageIndex = 1;
    this.queryData();
  }

  queryData(): void {
    this.userHttpService.queryUserCps({
      pageIndex: this.pageIndex,
      pageSize: this.pageSize,
      mobile: this.mobile,
      userId: this.userId
    }, this.defaultHttpConfig).subscribe((res) => {
      this.dataList = res.data.dataList
      this.pageTotal = res.data.total;
    })
  }

  save(data: any): void {
    this.userHttpService.updateUserCps({
      rateId: data.rateId,
      userRate: data.userRate,
      memo: data.memo
    }, this.defaultHttpConfig).subscribe((res) => {
      this.queryDataInit();
    })
  }

  add(): void {
    this.modalService.create({
      nzTitle: '添加',
      nzMaskClosable: false,
      nzContent: UserDetailComponent,
      nzFooter: null,
    }).afterClose.subscribe((): void => {
      this.queryData();
    });
  }
}
